/**
 * File: Main_Form.java
 * Created: Feb 12, 2007 3:33:19 PM
 */
package com;

import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;

import javax.swing.DefaultListModel;
import javax.swing.JToolBar;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JSplitPane;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import javax.swing.JScrollPane;
import javax.swing.JList;
import java.util.ArrayList;

import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.SwingConstants;

/**
 * @author Wes Billman
 * 
 */
public class Main_Form extends JFrame {

    private static final long serialVersionUID = 1L;

    private JPanel jContentPane = null;

    private JToolBar statusBar = null;

    private JLabel text_Status = null;

    private JToolBar toolBar_Main = null;

    private JButton but_UpdateAll = null;

    private JSplitPane splitPane_Main = null;

    private JSplitPane splitPane_Addons = null;

    private JPanel panel_Installed = null;

    private JPanel panel_Available = null;

    private JPanel panel_InstalledTop = null;

    private JLabel Installed = null;

    private JPanel panel_AvailableTop = null;

    private JLabel jLabel = null;

    private JScrollPane jScrollPane = null;

    private JScrollPane jScrollPane1 = null;

    private JList list_Installed = null;

    private JList list_Available = null;

    private JTabbedPane tabBottom = null;

    private JPanel panel_Details = null;

    private JPanel panel_Output = null;

    private JScrollPane jScrollPane2 = null;

    private JTextArea text_Output = null;

    private JTextField text_Filter = null;

    /**
         * My Variables
         */
    private ArrayList<WowAceAddon> m_availableAddons;

    /**
         * This method initializes statusBar
         * 
         * @return javax.swing.JToolBar
         */
    private JToolBar getStatusBar() {
	if (statusBar == null) {
	    text_Status = new JLabel();
	    text_Status.setText("Status");
	    statusBar = new JToolBar();
	    statusBar.setFloatable(false);
	    statusBar.add(text_Status);
	}
	return statusBar;
    }

    /**
         * This method initializes toolBar_Main
         * 
         * @return javax.swing.JToolBar
         */
    private JToolBar getToolBar_Main() {
	if (toolBar_Main == null) {
	    toolBar_Main = new JToolBar();
	    toolBar_Main.setFloatable(false);
	    toolBar_Main.add(getBut_UpdateAll());
	}
	return toolBar_Main;
    }

    /**
         * This method initializes but_UpdateAll
         * 
         * @return javax.swing.JButton
         */
    private JButton getBut_UpdateAll() {
	if (but_UpdateAll == null) {
	    but_UpdateAll = new JButton();
	    but_UpdateAll.setText("Update All");
	}
	return but_UpdateAll;
    }

    /**
         * This method initializes splitPane_Main
         * 
         * @return javax.swing.JSplitPane
         */
    private JSplitPane getSplitPane_Main() {
	if (splitPane_Main == null) {
	    splitPane_Main = new JSplitPane();
	    splitPane_Main.setOrientation(JSplitPane.VERTICAL_SPLIT);
	    splitPane_Main.setOneTouchExpandable(false);
	    splitPane_Main.setTopComponent(getSplitPane_Addons());
	    splitPane_Main.setBottomComponent(getTabBottom());
	    splitPane_Main.setResizeWeight(0.7D);
	}
	return splitPane_Main;
    }

    /**
         * This method initializes splitPane_Addons
         * 
         * @return javax.swing.JSplitPane
         */
    private JSplitPane getSplitPane_Addons() {
	if (splitPane_Addons == null) {
	    splitPane_Addons = new JSplitPane();
	    splitPane_Addons.setResizeWeight(0.5D);
	    splitPane_Addons.setRightComponent(getPanel_Available());
	    splitPane_Addons.setLeftComponent(getPanel_Installed());
	}
	return splitPane_Addons;
    }

    /**
         * This method initializes panel_Installed
         * 
         * @return javax.swing.JPanel
         */
    private JPanel getPanel_Installed() {
	if (panel_Installed == null) {
	    panel_Installed = new JPanel();
	    panel_Installed.setLayout(new BorderLayout());
	    panel_Installed.add(getPanel_InstalledTop(), BorderLayout.NORTH);
	    panel_Installed.add(getJScrollPane(), BorderLayout.CENTER);
	}
	return panel_Installed;
    }

    /**
         * This method initializes panel_Available
         * 
         * @return javax.swing.JPanel
         */
    private JPanel getPanel_Available() {
	if (panel_Available == null) {
	    panel_Available = new JPanel();
	    panel_Available.setLayout(new BorderLayout());
	    panel_Available.add(getPanel_AvailableTop(), BorderLayout.NORTH);
	    panel_Available.add(getJScrollPane1(), BorderLayout.CENTER);
	}
	return panel_Available;
    }

    /**
         * This method initializes panel_InstalledTop
         * 
         * @return javax.swing.JPanel
         */
    private JPanel getPanel_InstalledTop() {
	if (panel_InstalledTop == null) {
	    Installed = new JLabel();
	    Installed.setText("Installed Addons");
	    Installed.setPreferredSize(new Dimension(106, 22));
	    panel_InstalledTop = new JPanel();
	    panel_InstalledTop.setLayout(new BorderLayout());
	    panel_InstalledTop.add(Installed, BorderLayout.SOUTH);
	}
	return panel_InstalledTop;
    }

    /**
         * This method initializes panel_AvailableTop
         * 
         * @return javax.swing.JPanel
         */
    private JPanel getPanel_AvailableTop() {
	if (panel_AvailableTop == null) {
	    jLabel = new JLabel();
	    jLabel.setText("Available Addons");
	    panel_AvailableTop = new JPanel();
	    panel_AvailableTop.setLayout(new BorderLayout());
	    panel_AvailableTop.add(jLabel, BorderLayout.WEST);
	    panel_AvailableTop.add(getText_Filter(), BorderLayout.EAST);
	}
	return panel_AvailableTop;
    }

    /**
         * This method initializes jScrollPane
         * 
         * @return javax.swing.JScrollPane
         */
    private JScrollPane getJScrollPane() {
	if (jScrollPane == null) {
	    jScrollPane = new JScrollPane();
	    jScrollPane.setViewportView(getList_Installed());
	}
	return jScrollPane;
    }

    /**
         * This method initializes jScrollPane1
         * 
         * @return javax.swing.JScrollPane
         */
    private JScrollPane getJScrollPane1() {
	if (jScrollPane1 == null) {
	    jScrollPane1 = new JScrollPane();
	    jScrollPane1.setViewportView(getList_Available());
	}
	return jScrollPane1;
    }

    /**
         * This method initializes list_Installed
         * 
         * @return javax.swing.JList
         */
    private JList getList_Installed() {
	if (list_Installed == null) {
	    list_Installed = new JList();
	}
	return list_Installed;
    }

    /**
         * This method initializes list_Available
         * 
         * @return javax.swing.JList
         */
    private JList getList_Available() {
	if (list_Available == null) {
	    list_Available = new JList();
	    list_Available
		    .addListSelectionListener(new javax.swing.event.ListSelectionListener() {
			public void valueChanged(
				javax.swing.event.ListSelectionEvent e) {
			    availableAddonClicked();
			}
		    });
	}
	return list_Available;
    }

    protected void availableAddonClicked() {
	if (m_availableAddons.size() > 0) {

	}
    }

    /**
         * This method initializes tabBottom
         * 
         * @return javax.swing.JTabbedPane
         */
    private JTabbedPane getTabBottom() {
	if (tabBottom == null) {
	    tabBottom = new JTabbedPane();
	    tabBottom.setTabPlacement(JTabbedPane.TOP);
	    tabBottom.addTab("Details", null, getPanel_Details(), null);
	    tabBottom.addTab("Output", null, getPanel_Output(), null);
	}
	return tabBottom;
    }

    /**
         * This method initializes panel_Details
         * 
         * @return javax.swing.JPanel
         */
    private JPanel getPanel_Details() {
	if (panel_Details == null) {
	    GridLayout gridLayout1 = new GridLayout();
	    gridLayout1.setRows(3);
	    gridLayout1.setColumns(2);
	    GridLayout gridLayout = new GridLayout();
	    gridLayout.setRows(3);
	    gridLayout.setHgap(1);
	    gridLayout.setColumns(2);
	    panel_Details = new JPanel();
	    panel_Details.setLayout(new BorderLayout());
	}
	return panel_Details;
    }

    /**
         * This method initializes panel_Output
         * 
         * @return javax.swing.JPanel
         */
    private JPanel getPanel_Output() {
	if (panel_Output == null) {
	    GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
	    gridBagConstraints1.fill = GridBagConstraints.BOTH;
	    gridBagConstraints1.gridy = 0;
	    gridBagConstraints1.weightx = 1.0;
	    gridBagConstraints1.weighty = 1.0;
	    gridBagConstraints1.gridx = 0;
	    panel_Output = new JPanel();
	    panel_Output.setLayout(new GridBagLayout());
	    panel_Output.add(getJScrollPane2(), gridBagConstraints1);
	}
	return panel_Output;
    }

    /**
         * This method initializes jScrollPane2
         * 
         * @return javax.swing.JScrollPane
         */
    private JScrollPane getJScrollPane2() {
	if (jScrollPane2 == null) {
	    jScrollPane2 = new JScrollPane();
	    jScrollPane2.setViewportView(getText_Output());
	}
	return jScrollPane2;
    }

    /**
         * This method initializes text_Output
         * 
         * @return javax.swing.JTextArea
         */
    private JTextArea getText_Output() {
	if (text_Output == null) {
	    text_Output = new JTextArea();
	    text_Output.setLineWrap(false);
	    text_Output.setWrapStyleWord(false);
	}
	return text_Output;
    }

    /**
         * This method initializes text_Filter
         * 
         * @return javax.swing.JTextField
         */
    private JTextField getText_Filter() {
	if (text_Filter == null) {
	    text_Filter = new JTextField();
	    text_Filter.setText("Filter...");
	    text_Filter.setMaximumSize(new Dimension(125, 2147483647));
	    text_Filter.setPreferredSize(new Dimension(125, 22));
	}
	return text_Filter;
    }

    /**
         * @param args
         */
    public static void main(String[] args) {
	// TODO Auto-generated method stub
	SwingUtilities.invokeLater(new Runnable() {
	    public void run() {
		Main_Form thisClass = new Main_Form();
		thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		thisClass.setVisible(true);
	    }
	});
    }

    /**
         * This is the default constructor
         */
    public Main_Form() {
	super();
	initialize();
    }

    /**
         * This method initializes this
         * 
         * @return void
         */
    private void initialize() {
	this.setSize(627, 473);
	this.setContentPane(getJContentPane());
	this.setTitle("JFrame");
	this.addWindowListener(new java.awt.event.WindowAdapter() {
	    public void windowOpened(java.awt.event.WindowEvent e) {
		refreshAllAddons();
	    }
	});
    }

    protected void refreshAllAddons() {
	Thread t = new Thread(new AllAddonRefresher());
	t.setName("All Addon Refresher Thread");
	t.start();
    }

    private class AllAddonRefresher implements Runnable {
	public void run() {
	    String indexFileString;
	    tabBottom.setSelectedIndex(1);

	    WebConnector conn = new WebConnector("192.232.119.194", "81", false);

	    WriteOutputMessage("Checking wowace for latest addons list...\n");
	    indexFileString = conn
		    .getIndexFile("http://files.wowace.com/descript.ion");

	    if (indexFileString.equals("ERROR")) {
		WriteOutputMessage("Unable to locate wowace addon server.\n");
		WriteOutputMessage("Please check your internet connection (and maybe proxy settings)\n");
		return;
	    }

	    WriteOutputMessage("Found file.\n");

	    WriteOutputMessage("Parsing addons list...");
	    m_availableAddons = AddonListParser.ParseAddonList(indexFileString);

	    DefaultListModel listModel = new DefaultListModel();
	    for (int i = 0; i < m_availableAddons.size(); i++) {
		listModel.addElement(m_availableAddons.get(i).getName());
	    }

	    DefaultListModel listMode2 = new DefaultListModel();
	    for (int i = 0; i < 50; i++) {
		listMode2.addElement(" I " + i);
	    }
	    list_Available.setModel(listModel);
	    list_Available.setDragEnabled(true);
	    list_Installed.setModel(listMode2);
	    list_Installed.setTransferHandler(new ListTransferHandler());

	    tabBottom.setSelectedIndex(0);
	}
    }

    private void WriteOutputMessage(String message) {
	this.text_Output.append("- " + message);
    }

    /**
         * This method initializes jContentPane
         * 
         * @return javax.swing.JPanel
         */
    private JPanel getJContentPane() {
	if (jContentPane == null) {
	    jContentPane = new JPanel();
	    jContentPane.setLayout(new BorderLayout());
	    jContentPane.add(getStatusBar(), BorderLayout.SOUTH);
	    jContentPane.add(getToolBar_Main(), BorderLayout.NORTH);
	    jContentPane.add(getSplitPane_Main(), BorderLayout.CENTER);
	}
	return jContentPane;
    }

} // @jve:decl-index=0:visual-constraint="10,10"
